package com.prs.crm.action.cqunicom;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.Sender;
import com.prs.crm.exception.InValidDeleteException;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.SenderService;
import com.prs.crm.service.sys.LogService;

/**
 * @author  WangTing
 *
 * 创建时间：2011-3-5 
 * 
 */

public class SenderAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private Sender sender;
	private SenderService senderService;
	private LogSerchService logSerchService;
	private Integer[] senderIds;
  
	
	private PaginationSupport pagination;
	private LogService logService;
	
	@Action("editSender")
	public String edit() {
		return SUCCESS;
	}
	
	@Action("viewSender")
	public String view() {
		return SUCCESS;
	}
	
	@Action("listSender")
	public String listSender() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Sender.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSenderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("alterSender")
	public String alterSender() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Sender.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSenderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveSender", results = {
			@Result(name = "success", type = "redirect", location = "editSender"),
			@Result(name = "none", type = "redirect", location = "listSender"),
			@Result(name = "error", type = "chain", location = "editSender") })
	public String save() {
		
		/*if (sender.getName()== null || "".equals(sender.getName())) {
			this.addActionError("姓名不能为空");
			return ERROR;
		}
		
		if (sender.getPhone_No()== null || "".equals(sender.getPhone_No())) {
			this.addActionError("联系电话不能为空");
			return ERROR;
		}
		
		if (istel(sender.getPhone_No())) {
				this.addActionError("电话 格式不对！请重新输入");
				return ERROR;
			
		}
		if (sender.getCompany()== null || "".equals(sender.getCompany())) {
			this.addActionError("所属公司不能为空");
			return ERROR;
		}
		*/
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("创建了新的送票员");
		this.getLogSerchService().save(log);
		 this.getSenderService().save(sender);
		   
		   
		   return SUCCESS;
	}

	@Action(value = "removeSender", results = {
			@Result(name = "success", type = "redirect", location = "listSender"),
			@Result(name = "error", type = "chain", location = "listSender") })
	public String remove() {
		try {
			if (senderIds == null) {
				this.addActionError("请选择要删除的送票员");
				return ERROR;
			}
			this.getSenderService().batchRemove(senderIds);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			return ERROR;
		}
	}
	
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public Sender getSender() {
		return sender;
	}

	public void setSender(Sender sender) {
		this.sender = sender;
	}

	public SenderService getSenderService() {
		return senderService;
	}

	public void setSenderService(SenderService senderService) {
		this.senderService = senderService;
	}

	public Integer[] getSenderIds() {
		return senderIds;
	}

	public void setSenderIds(Integer[] senderIds) {
		this.senderIds = senderIds;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public boolean istel(String str) {
		boolean tag = false;  
		final String pattern1 = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public LogSerchService getLogSerchService() {
		return logSerchService;
	}

	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	
	

}
